#include "stdafx.h"

using namespace Ogre;


namespace HeightFunction
{
	Real pos[6][6]={21,21,19.5,19.6,19,19,
				   19,20,19.2,19.5,19.2,20,
				   20,20.2,21,21.5,21,20,
				  20,20.5,22.5,22.4,22,20, 
				   20,20.5,22,22,21,20,
				   21,21,19.5,19.6,19,19};

	int border[2]={22,318};
		int maxX=51/5;
		int maxZ=59/5;
		inline float getTerrainHeight(const float x, const float z, void *userData = NULL){
		Real x0=(x-border[0])/maxX;
	    Real z0=(z-border[1])/maxZ;
		int xC=Math::Ceil(x0);
	    int zC=Math::Ceil(z0);
		int xF=Math::Floor(x0);
		int zF=Math::Floor(z0);
		Real xp=x0-xF;
		Real zp=z0-zF;
		Real xpc=xC-x0;
		Real zpc=zC-z0;
		int y=pos[xF][zF]*(1-xp)*(1-zp)+pos[xC][zC]*(1-xpc)*(1-zpc)+pos[xF][zC]*(1-xp)*(1-zpc)+pos[xC][zF]*(1-xpc)*(1-zp);
		return y;
	}
}
NxOgre::Scene* mSc=NULL;

namespace HeightFunction2
{
   Real pos[11][11]={2, 2,  2,   2,  2,    3,  2,   2, 2,   2, 2,
				     2, 3.5,  4 ,  4,  4,    5,  4 ,  4, 4,   3.5, 2,
				     2, 4,  5.5,   6,  6, 7.5,   6,   6, 5.5,   4, 2,
				     2, 4,  6  , 8, 8.5,  9,   8.5, 8, 6  , 4, 2,
				     2, 4,  6, 8.5, 10,  10.5, 10 , 8.5, 6, 4, 3,
				     3, 5,  7.5, 9, 10.5,11,  10.5, 9, 7.5, 5, 3,///
				     2, 4,  6, 8.5, 10,  10.5, 10 , 8.5, 6, 4, 3,
					 2, 4,  6  , 8, 8.5,  9,   8.5, 8, 6  , 4, 2,
					 2, 4,  5.5,   6,  6, 7.5,   6,   6, 5.5,   4, 2,
					 2, 3.5,  4 ,  4,  4,    5,  4 ,  4, 4,   3.5, 2,
					 2, 2,  2,   2,  2,    3,  2,   2, 2,   2, 2};

	int border[2]={-20,-20};
		int maxX=40/10;
		int maxZ=40/10;
		inline float getTerrainHeight(const float x, const float z, void *userData = NULL){
		Real d=0;	


		NxOgre::Ray ray;
		ray.mOrigin=NxOgre::Vec3(x,0,z);
		ray.mDirection=NxOgre::Vec3(0,-1,0);
		NxOgre::RaycastHit rh= mSc->raycastClosestShape(ray,NxOgre::Enums::ShapesType_Static);
		
		if(rh.mDistance<100)
		{
		d=-1*rh.mDistance;
		}

/*
		OgreNewt::BasicRaycast* ray = new OgreNewt::BasicRaycast( www,Vector3(x,20,z),Vector3(x,-1,z)  ,true);
		OgreNewt::BasicRaycast::BasicRaycastInfo info = ray->getInfoAt(0);

			if (info.mBody)
			{
				d=20+((-21)*info.mDistance);
			}

		delete ray;*/

		return d;
	}
}

void createMenuLevel(SceneManager* mSceneMgr,NxOgre::World* m_World, NxOgre::Scene* m_Scene,Critter::RenderSystem*  m_RenderSystem,LvlUpdate** lvlUp)
{
	mSc=m_Scene;
	//loadScene("../../media/menu.scene",mSceneMgr,mWorld,mScene,mRenderSystem,flag_mat);
	mSceneMgr->setSkyBox(true, "TCENoonSkyBox");
	Camera* mCamera=mSceneMgr->getCamera("Camera"); 
	SceneNode* camRotator=mSceneMgr->getRootSceneNode()->createChildSceneNode("camRotator");
	camRotator->attachObject(mCamera);
	camRotator->setPosition(Vector3(0,15,0));
	mCamera->setPosition(Vector3(-15,-9,15));
	mCamera->lookAt(Vector3(0,15,0));
	camRotator->yaw(Ogre::Degree(-65));
	
	Forests::PagedGeometry *grass = new Forests::PagedGeometry(mSceneMgr->getCamera("Camera"), 50);
	grass->addDetailLevel<Forests::GrassPage>(200,20);
	Forests::GrassLoader *grassLoader = new Forests::GrassLoader(grass);
	grassLoader->setHeightFunction(&HeightFunction2::getTerrainHeight);
	grass->setPageLoader(grassLoader);
	Forests::GrassLayer *layer = grassLoader->addLayer("grass");
	layer->setMinimumSize(0.03f, 0.03f);
	layer->setMaximumSize(0.05f, 0.05f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(1.8f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(-30, -30,30,30));
	//layer->setColorMap("lm.png");
	layer = grassLoader->addLayer("grass2");
	layer->setMinimumSize(0.2f, 0.2f);
	layer->setMaximumSize(0.4f, 0.4f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(0.3f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(-30, -30,30,30));

	*lvlUp= new LvlMenuUpdate(camRotator,grass);
}

void createLevel2(SceneManager* mSceneMgr, NxOgre::World* m_World, NxOgre::Scene* m_Scene,Critter::RenderSystem*  m_RenderSystem,LvlUpdate** lvlUp, Player* p)
{

	loadScene("../../media/testxml.scene",mSceneMgr,m_Scene,m_RenderSystem);
	mSceneMgr->setSkyBox(true, "TCENoonSkyBox");

	Forests::PagedGeometry *trees = new Forests::PagedGeometry(mSceneMgr->getCamera("Camera"), 50);
	trees->addDetailLevel<Forests::BatchPage>(150, 30);
	trees->addDetailLevel<Forests::ImpostorPage>(400, 50);
	Forests::TreeLoader3D *treeLoader = new Forests::TreeLoader3D(trees, Forests::TBounds(0, 0, 1500, 1500));
	trees->setPageLoader(treeLoader);

	mSc=m_Scene;
	Forests::PagedGeometry *grass = new Forests::PagedGeometry(mSceneMgr->getCamera("Camera"), 50);
	grass->addDetailLevel<Forests::GrassPage>(100,25);
	Forests::GrassLoader *grassLoader = new Forests::GrassLoader(grass);
	grassLoader->setHeightFunction(&HeightFunction2::getTerrainHeight);
	grass->setPageLoader(grassLoader);
	Forests::GrassLayer *layer = grassLoader->addLayer("grassHop");
	layer->setMinimumSize(1.5f, 1.5f);
	layer->setMaximumSize(2.0f, 2.0f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(3.0f);
	layer->setSwayLength(0.3f);
	layer->setSwaySpeed(0.3f);
	layer->setDensity(2.0f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(-300, -300,300,300));
	//layer->setColorMap("lm.png");


	int o=0;
	for(int i=0;i<6;i++)
		for(int j=0;j<3
			;j++)
		{
			NxOgre::Ray ray;
		ray.mOrigin=NxOgre::Vec3(i*15,0,j*15);
		ray.mDirection=NxOgre::Vec3(0,-1,0);
		NxOgre::RaycastHit rh= mSc->raycastClosestShape(ray,NxOgre::Enums::ShapesType_Static);
		
		if(rh.mDistance<100)
		{
		//treeLoader->addTree(myTree, Vector3(i*15, -1*rh.mDistance+7.5, j*15), Degree(0), 1);
			Entity *myTree = mSceneMgr->createEntity("MyTree"+Ogre::StringConverter::toString(o++), "default.mesh");
			Ogre::SceneNode* nn=mSceneMgr->getRootSceneNode()->createChildSceneNode(Vector3(i*15, -1*rh.mDistance+7.5, j*15));
			nn->attachObject(myTree);
			nn->yaw(Ogre::Radian(Ogre::Degree(180)));

		}
			
		}
/*
	Forests::PagedGeometry *grass = new Forests::PagedGeometry(mSceneMgr->getCamera("Camera"), 50);
	grass->addDetailLevel<Forests::GrassPage>(200,20);
	Forests::GrassLoader *grassLoader = new Forests::GrassLoader(grass);
	grassLoader->setHeightFunction(&HeightFunction::getTerrainHeight);
	grass->setPageLoader(grassLoader);*/
			
		/*SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
        Animation* anim = mSceneMgr->createAnimation("CameraTrack", 40);
        anim->setInterpolationMode(Animation::IM_SPLINE);
        NodeAnimationTrack* track = anim->createNodeTrack(0, node);
        track->createNodeKeyFrame(0)->setTranslate(Vector3(200, 100, 0));
        track->createNodeKeyFrame(10)->setTranslate(Vector3(0, 100, 200));
        track->createNodeKeyFrame(20)->setTranslate(Vector3(-200, 100, 0));
        track->createNodeKeyFrame(30)->setTranslate(Vector3(0, 100, -200));
        track->createNodeKeyFrame(40)->setTranslate(Vector3(200, 100, 0));
        AnimationState* mAnimState = mSceneMgr->createAnimationState("CameraTrack");
        mAnimState->setEnabled(true);
		p->setBaloonTrack(node);*/

		//Baloon* b=new Baloon(mSceneMgr,mWorld,Vector3(0,20,0));



	/*Forests::GrassLayer *layer = grassLoader->addLayer("grass");
	layer->setMinimumSize(2.0f, 2.0f);
	layer->setMaximumSize(2.5f, 2.5f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(1.5f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(22, 318,81,368));
	layer->setDensityMap("teren.png");
	//layer->setColorMap("lm.png");
	layer = grassLoader->addLayer("grass");
	layer->setMinimumSize(2.0f, 2.0f);
	layer->setMaximumSize(2.5f, 2.5f);
	layer->setAnimationEnabled(true);
	layer->setSwayDistribution(10.0f);
	layer->setSwayLength(0.5f);
	layer->setSwaySpeed(0.5f);
	layer->setDensity(0.5f);
	layer->setFadeTechnique(Forests::FADETECH_ALPHA);
	layer->setMapBounds(Forests::TBounds(22, 318,81,368));
	//layer->setColorMap("lm.png");
	layer->setDensityMap("teren.png");*/
	*lvlUp= new Lvl2Update(mSceneMgr,grass,trees,NULL);


}